1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| import Decimal from 'decimal.js-light'
Decimal.set({ precision: 20, rounding: Decimal.ROUND_HALF_UP,//默认使用四舍五入 toExpNeg: -9e15, toExpPos: 9e15, });
export default { //数字转换为字符串显示 format(num){ num = num ? num : 0 return new Decimal(num).toString() }, //保留小数向上取整 formatUp(num,len){ return new Decimal(num).toDecimalPlaces(len,Decimal.ROUND_UP).toString() }, //保留小数向下取整 formatDown(num,len){ return new Decimal(num).toDecimalPlaces(len,Decimal.ROUND_DOWN).toString() }, //保留小数四舍五入 formatHalf(num,len){ return new Decimal(num).toDecimalPlaces(len,Decimal.ROUND_HALF_UP).toString() }, //小数舍去0 remove0(num){ num = num ? num : 0 return new Decimal(num).toString() }, //小数补零 add0(num,len){ return new Decimal(num).toFixed(len,Decimal.ROUND_DOWN).toString() }, //加 add(num1, num2, len) { let a = new Decimal(num1) let b = new Decimal(num2) let c = a.plus(b) if(len > 0){ return c.toDecimalPlaces(len).toString() }else{ return c.toString() } }, //减 minus(num1, num2, len) { let a = new Decimal(num1) let b = new Decimal(num2) let c = a.minus(b) if(len > 0){ return c.toDecimalPlaces(len).toString() }else{ return c.toString() } }, //乘 mult(num1, num2, len) { let a = new Decimal(num1) let b = new Decimal(num2) let c = a.times(b) if(len > 0){ return c.toDecimalPlaces(len).toString() }else{ return c.toString() } }, //除 div(num1, num2, len) { let a = new Decimal(num1) let b = new Decimal(num2) let c = a.dividedBy(b) if(len > 0){ return c.toDecimalPlaces(len).toString() }else{ return c.toString() } }, //取余 mod(num1, num2, len) { let a = new Decimal(num1) let b = new Decimal(num2) let c = a.modulo(b) if(len > 0){ return c.toDecimalPlaces(len).toString() }else{ return c.toString() } } }
|